package com.myself.community.utils;

import com.myself.community.entity.User;
import org.springframework.stereotype.Component;

/**
 * 持有用户信息,用于代替session对象。基于ThreadLocal封装的工具类，实现了线程隔离
 */
@Component
public class HostHolder {

    private ThreadLocal<User> users = new ThreadLocal<>();

    /**
     * 存储当前线程的的用户
     * @param user
     */
    public void setUser(User user) {
        users.set(user);
    }

    /**
     *取出用户
     * @return
     */
    public User getUser() {
        return users.get();
    }

    /**
     * 清理当前线程中存储的信息
     */
    public void clear() {
        users.remove();
    }

}
